Proper connect_port
[juce-lv2.git] / juce / source / extras / the jucer / src / model / components / jucer_GenericComponentHandler.h
blobd7109b31b9876fdac02cea37b85c3caa12ff0c5f
1 /*
2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCER_GENERICCOMPONENTHANDLER_JUCEHEADER__
27 #define __JUCER_GENERICCOMPONENTHANDLER_JUCEHEADER__
30 //==============================================================================
31 /**
33 class GenericComponent : public Component
35 public:
36 GenericComponent()
37 : Component ("new component"),
38 actualClassName ("Component")
42 void paint (Graphics& g)
44 g.fillAll (Colours::white.withAlpha (0.25f));
46 g.setColour (Colours::black.withAlpha (0.5f));
47 g.drawRect (0, 0, getWidth(), getHeight());
48 g.drawLine (0.0f, 0.0f, (float) getWidth(), (float) getHeight());
49 g.drawLine (0.0f, (float) getHeight(), (float) getWidth(), 0.0f);
51 g.setFont (14.0f);
52 g.drawText (actualClassName, 0, 0, getWidth(), getHeight() / 2, Justification::centred, true);
55 void setClassName (const String& newName)
57 if (actualClassName != newName)
59 actualClassName = newName;
60 repaint();
64 void setParams (const String& newParams)
66 if (constructorParams != newParams)
68 constructorParams = newParams;
69 repaint();
73 String actualClassName, constructorParams;
76 //==============================================================================
77 /**
79 class GenericComponentHandler : public ComponentTypeHandler
81 public:
82 //==============================================================================
83 GenericComponentHandler()
84 : ComponentTypeHandler ("Generic Component", "GenericComponent", typeid (GenericComponent), 150, 24)
87 //==============================================================================
88 Component* createNewComponent (JucerDocument*)
90 return new GenericComponent();
93 XmlElement* createXmlFor (Component* comp, const ComponentLayout* layout)
95 XmlElement* e = ComponentTypeHandler::createXmlFor (comp, layout);
96 e->setAttribute ("class", ((GenericComponent*) comp)->actualClassName);
97 e->setAttribute ("params", ((GenericComponent*) comp)->constructorParams);
99 return e;
102 bool restoreFromXml (const XmlElement& xml, Component* comp, const ComponentLayout* layout)
104 if (! ComponentTypeHandler::restoreFromXml (xml, comp, layout))
105 return false;
107 ((GenericComponent*) comp)->actualClassName = xml.getStringAttribute ("class", "Component");
108 ((GenericComponent*) comp)->constructorParams = xml.getStringAttribute ("params", String::empty);
109 return true;
112 void getEditableProperties (Component* component, JucerDocument& document, Array <PropertyComponent*>& properties)
114 ComponentTypeHandler::getEditableProperties (component, document, properties);
116 properties.add (new GenericCompClassProperty (dynamic_cast <GenericComponent*> (component), document));
117 properties.add (new GenericCompParamsProperty (dynamic_cast <GenericComponent*> (component), document));
120 const String getClassName (Component* comp) const
122 return ((GenericComponent*) comp)->actualClassName;
125 const String getCreationParameters (Component* comp)
127 return ((GenericComponent*) comp)->constructorParams;
130 void fillInCreationCode (GeneratedCode& code, Component* component, const String& memberVariableName)
132 ComponentTypeHandler::fillInCreationCode (code, component, memberVariableName);
134 if (component->getName().isNotEmpty())
135 code.constructorCode
136 << memberVariableName << "->setName ("
137 << quotedString (component->getName())
138 << ");\n\n";
139 else
140 code.constructorCode << "\n";
143 private:
144 class GenericCompClassProperty : public ComponentTextProperty <GenericComponent>
146 public:
147 GenericCompClassProperty (GenericComponent* comp, JucerDocument& document)
148 : ComponentTextProperty <GenericComponent> ("class", 300, false, comp, document)
152 void setText (const String& newText)
154 document.perform (new GenericCompClassChangeAction (component, *document.getComponentLayout(),
155 makeValidCppIdentifier (newText, false, false, true)),
156 "Change generic component class");
159 const String getText() const
161 return component->actualClassName;
164 private:
165 class GenericCompClassChangeAction : public ComponentUndoableAction <GenericComponent>
167 public:
168 GenericCompClassChangeAction (GenericComponent* const comp, ComponentLayout& layout, const String& newState_)
169 : ComponentUndoableAction <GenericComponent> (comp, layout),
170 newState (newState_)
172 oldState = comp->actualClassName;
175 bool perform()
177 showCorrectTab();
178 getComponent()->setClassName (newState);
179 changed();
180 return true;
183 bool undo()
185 showCorrectTab();
186 getComponent()->setClassName (oldState);
187 changed();
188 return true;
191 String newState, oldState;
195 class GenericCompParamsProperty : public ComponentTextProperty <GenericComponent>
197 public:
198 GenericCompParamsProperty (GenericComponent* comp, JucerDocument& document)
199 : ComponentTextProperty <GenericComponent> ("constructor params", 1024, true, comp, document)
203 void setText (const String& newText)
205 document.perform (new GenericCompParamsChangeAction (component, *document.getComponentLayout(), newText),
206 "Change generic component class");
209 const String getText() const
211 return component->constructorParams;
214 private:
215 class GenericCompParamsChangeAction : public ComponentUndoableAction <GenericComponent>
217 public:
218 GenericCompParamsChangeAction (GenericComponent* const comp, ComponentLayout& layout, const String& newState_)
219 : ComponentUndoableAction <GenericComponent> (comp, layout),
220 newState (newState_)
222 oldState = comp->constructorParams;
225 bool perform()
227 showCorrectTab();
228 getComponent()->setParams (newState);
229 changed();
230 return true;
233 bool undo()
235 showCorrectTab();
236 getComponent()->setParams (oldState);
237 changed();
238 return true;
241 String newState, oldState;
247 #endif // __JUCER_GENERICCOMPONENTHANDLER_JUCEHEADER__